package com.augn.utils;

import com.augn.entity.InvestmentProgress;
import com.augn.service.IInvestmentProgressService;
import com.augn.service.impl.InvestmentProgressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

@Configuration      // 主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 开启定时任务
public class ScheduledUtils {

    private List<InvestmentProgress> investmentProgressList;
    @Autowired
    private IInvestmentProgressService service = new InvestmentProgressServiceImpl();

    // 每天零点更新一次所有未结束的招商进度
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateProcess() {
        investmentProgressList = service.getPartInvestmentProgress();
        for (InvestmentProgress investmentProgress : investmentProgressList) {
            String process = investmentProgress.getProgress();
            Date date = new Date();
            if (!process.equals("end")) {
                if (process.equals("start")) {
                    if(date.compareTo(investmentProgress.getStartTime())>0) {
                        investmentProgress.setProgress("talk");
                    }
                } else if (process.equals("talk")) {
                    if(date.compareTo(investmentProgress.getTalkTime())>0) {
                        investmentProgress.setProgress("enter");
                    }
                } else if (process.equals("enter")) {
                    if(date.compareTo(investmentProgress.getEnterTime())>0) {
                        investmentProgress.setProgress("choose");
                    }
                } else if (process.equals("choose")) {
                    if(date.compareTo(investmentProgress.getChooseTime())>0) {
                        investmentProgress.setProgress("end");
                    }
                }
                service.updateOneInvestmentProgress(investmentProgress);
            }
        }
    }
}
